package cn.cimoc.util;

import cn.cimoc.constant.ErrorEnum;
import cn.cimoc.model.vo.ResultVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author LGZ
 * @date 2021/5/9 20:54
 * <p>
 * 普通工具类
 */
public class CommonUtils {


    public static ResultVO ajaxReturn(Object data, Integer code, String message) throws JsonProcessingException {
        return new ResultVO(data, code, message);
    }

    /**
     * TODO
     * @param data :
     */
    public static ResultVO ajaxReturn(Object data) {
        return new ResultVO(data, 2000, "成功");
    }

    /**
     * TODO

     * @param code :
     * @param message :
     */
    public static ResultVO ajaxReturn(Integer code,String message){
        return new ResultVO(null, code, message);
    }

    public static ResultVO ajaxReturn(ErrorEnum errorEnum) {
        return new ResultVO(null, errorEnum.getCode(), errorEnum.getMsg());
    }


    /**
     * <p>
     * 返回一个DefaultMapperFactory的build对象，主要用来实现entity与dto的转换
     * 使用举例：CommonUtil.getMapperFactory().getMapperFacade().map(实例对象，目标类.class)
     * 实际上build对象还有很多方法，可以根据百度学习
     * @return : ma.glasnost.orika.impl.DefaultMapperFactory
     */
    public static DefaultMapperFactory getMapperFactory(){
        return new DefaultMapperFactory.Builder().build();
    }

    public static MapperFacade getMapperFacade() {
        return getMapperFactory().getMapperFacade();
    }


    /**
     * TODO
     * @param time :
     * @return : java.lang.String
     */
    public static String getTime(Date time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(time);
    }

    /**
     * TODO
     * @param time :
     * @return : java.lang.String
     */
    public static String getTime(Date time, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(time);
    }

    /**
     * <p>
     * 随机生成UUID
     * @author lgz
     * @date 2021/11/21 20:56
     * @return : java.lang.String
     */
    public static String generateUUID() {
        return UUID.randomUUID().toString();
    }


}
